/* 
 * File:   Complexe.cpp
 * Author: Gab'z
 * 
 * Created on 1 mai 2012, 19:46
 **/

#include "Complexe.h"

Complexe* Complexe::clone() const {
    return new Complexe(_re, _im);
}

Complexe::~Complexe() {
    delete _re;
    delete _im;
}

//std::string Complexe::toString() const {
//    // utiliser un flux de sortie pour créer la chaîne
//    std::ostringstream oss;
//    // écrire la valeur dans le flux
//    oss << "(";
//    oss << _re->toString();
//    oss << "+";
//    oss << _im->toString();
//    oss << ".i)";
//    // renvoyer une string
//    return oss.str();
//}

std::string Complexe::toString() const {
    // utiliser un flux de sortie pour créer la chaîne
    std::ostringstream oss;
    // écrire la valeur dans le flux
    oss << _re->toString();
    oss << "$";
    oss << _im->toString();
    // renvoyer une string
    return oss.str();
}

Complexe& Complexe::operator+=(const Complexe& c) {
    NombreSimple* c_re = add(_re, c._re);
    NombreSimple* c_im = add(_im, c._im);
    delete _re;
    delete _im;
    _re = c_re;
    _im = c_im;
    return *this;
}

Complexe& Complexe::operator-=(const Complexe& c) {
    NombreSimple* c_re = sub(_re, c._re);
    NombreSimple* c_im = sub(_im, c._im);
    delete _re;
    delete _im;
    _re = c_re;
    _im = c_im;
    return *this;
}

Complexe& Complexe::operator*=(const Complexe& co) {
    NombreSimple* a = mul(_re, co._re);
    NombreSimple* b = mul(_im, co._im);
    NombreSimple* c = mul(_re, co._im);
    NombreSimple* d = mul(_im, co._re);
    NombreSimple* _re_c = sub(a, b);
    NombreSimple* _im_c = add(c, d);
    delete a;
    delete b;
    delete c;
    delete d;
    delete _re;
    delete _im;
    _re = _re_c;
    _im = _im_c;
    
    return *this;
}

Complexe& Complexe::operator/=(const Complexe& co) {
    NombreSimple* a = mul(_re, co._re);
    NombreSimple* b = mul(_im, co._im);
    NombreSimple* c = mul(co._re, co._re);
    NombreSimple* d = mul(co._im, co._im);
    NombreSimple* e = add(a, b);
    NombreSimple* f = add(c, d);
    NombreSimple* _re_c = div(e, f);

    NombreSimple* g = mul(_im, co._re);
    NombreSimple* h = mul(_re, co._im);
    NombreSimple* i = mul(co._re, co._re);
    NombreSimple* j = mul(co._im, co._im);
    NombreSimple* k = sub(g, h);
    NombreSimple* l = add(i, j);
    NombreSimple* _im_c = div(k, l);
    delete a;
    delete b;
    delete c;
    delete d;
    delete e;
    delete f;
    delete g;
    delete h;
    delete i;
    delete j;
    delete k;
    delete l;
    delete _re;
    delete _im;
    _re = _re_c;
    _im = _im_c;

    return *this;
}

Complexe operator+(const Complexe& c1, const Complexe& c2) {
    Complexe c(c1);
    c += c2;
    return c;
}

Complexe operator-(const Complexe& c1, const Complexe& c2) {
    Complexe c(c1);
    c -= c2;
    return c;
}

Complexe operator*(const Complexe& c1, const Complexe& c2) {
    Complexe c(c1);
    c *= c2;
    return c;
}

Complexe operator/(const Complexe& c1, const Complexe& c2) {
    Complexe c(c1);
    c /= c2;
    return c;
}